package com.wangshang.general.depot.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wangshang.general.depot.po.DepotInfo;
import com.wangshang.general.depot.po.DepotInfoId;

/**
 * A data access object (DAO) providing persistence and search support for
 * DepotInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wangshang.general.depot.po.DepotInfo
 * @author MyEclipse Persistence Tools
 */

public class DepotInfoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DepotInfoDAO.class);
	// property constants
	public static final String PLACE_MASK = "placeMask";
	public static final String COMMODITY_NAME = "commodityName";
	public static final String PLACE_IN_DEPOT = "placeInDepot";
	public static final String NOTICE = "notice";
	public static final String NUM_OF_COMMODITY = "numOfCommodity";

	protected void initDao() {
		// do nothing
	}

	public void save(DepotInfo transientInstance) {
		log.debug("saving DepotInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DepotInfo persistentInstance) {
		log.debug("deleting DepotInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DepotInfo findById(com.wangshang.general.depot.po.DepotInfoId id) {
		log.debug("getting DepotInfo instance with id: " + id);
		try {
			DepotInfo instance = (DepotInfo) getHibernateTemplate().get(
					"com.wangshang.general.depot.DepotInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(DepotInfo instance) {
		log.debug("finding DepotInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DepotInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from DepotInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPlaceMask(Object placeMask) {
		return findByProperty(PLACE_MASK, placeMask);
	}

	public List findByCommodityName(Object commodityName) {
		return findByProperty(COMMODITY_NAME, commodityName);
	}

	public List findByPlaceInDepot(Object placeInDepot) {
		return findByProperty(PLACE_IN_DEPOT, placeInDepot);
	}

	public List findByNotice(Object notice) {
		return findByProperty(NOTICE, notice);
	}

	public List findByNumOfCommodity(Object numOfCommodity) {
		return findByProperty(NUM_OF_COMMODITY, numOfCommodity);
	}

	public List findAll() {
		log.debug("finding all DepotInfo instances");
		try {
			String queryString = "from DepotInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public DepotInfo merge(DepotInfo detachedInstance) {
		log.debug("merging DepotInfo instance");
		try {
			DepotInfo result = (DepotInfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(DepotInfo instance) {
		log.debug("attaching dirty DepotInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(DepotInfo instance) {
		log.debug("attaching clean DepotInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DepotInfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (DepotInfoDAO) ctx.getBean("DepotInfoDAO");
	}
}